﻿@inherits LayoutComponentBase
@inject MessageService message;
@inject HttpClient HttpClient;
@inject IJSRuntime  JSRuntime
@inject NavigationManager navigationManager
@inject IConfiguration configure
<Layout>
    <div class="header-box">
        <div class="nav-link">
            <ul class="nav-link-ul">
                <li><a href="/">主页</a></li>
                @*<li><a href="/tools">工具</a></li>*@
            </ul>
        </div>
        @*<div class="nav-search-box">
                <form class="nav-searchform">
                    <input type="text" autocomplete="off" placeholder="搜索" class="nav-search-keyword" />
                </form>
            </div>*@
        <div class="nav-search-box">
            <Search Placeholder="" OnPressEnter="OnSearch" EnterButton="true" @bind-Value="@txtValue" />
        </div>
        <div class="nav-user-center">
            <div class="nav-user-center-item">
                <a @onclick ="@(()=>HeadNavTo())">
                    <Avatar Src="@HeadUrl" />
                </a>
            </div>
            <div class="nav-user-center-item"><span>消息</span></div>
            <div class="nav-user-center-item"><span>历史</span></div>
            <div @onclick="e=>Logout()" class="nav-user-center-item" style="display:@logoutDisply"><span><a>注销</a></span></div>
        </div>
    </div>
    <div class="layout-body">
        @Body
    </div>
    <Footer Style="text-align: center;">Ant Design ©2018 Created by Ant UED</Footer>
</Layout>

<style>
    /*    #components-layout-demo-fixed .logo {
        width: 120px;
        height: 31px;
        background: rgba(255, 255, 255, 0.2);
        margin: 16px 24px 16px 0;
        float: left;
    }*/

    .header-box {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        box-sizing: border-box;
        padding: 10px 24px;
        line-height: 30px;
        position: relative;
        font: 16px -apple-system,BlinkMacSystemFont,Helvetica Neue,Helvetica,Arial,PingFang SC,Hiragino Sans GB,Microsoft YaHei,sans-serif;
        height: 155px;
        width: 100%;
        background-image: url('../img/backimg.jpg');
        background-size: 100% 100%;
        background-repeat: no-repeat;
    }

    .nav-link-ul {
        display: flex;
        align-items: center;
        height: 36px;
    }

    .nav-search-box {
        width: 400px;
    }

    .nav-user-center {
        display: flex;
        align-items: center;
    }

    .nav-user-center-item {
        margin-left: 14px;
    }

    .layout-body {
        background-color: white;
    }

    ol, li, ui {
        margin: 0;
        padding: 0;
        list-style: none;
        margin-right: 14px;
    }

    a {
        color: inherit;
    }
</style>

@code{
    public string txtValue { get; set; }
    public UserInfoDto userInfo { get; set; }
    public string HeadUrl { get; set; }
    public string token { get; set; }
    public string logoutDisply { get; set; }
    public string SelfUrl => configure.GetSection("App")["SelfUrl"];
    public string AuthUrl => configure.GetSection("AuthServer")["Authority"];

    protected override async Task OnInitializedAsync()
    {
        token = await JSRuntime.InvokeAsync<string>("localStorage.getItem", "Authorization");
        //token = await sessionStorage.GetItemAsync<string>("Authorization");
        if (!string.IsNullOrEmpty(token))
        {
            if (!HttpClient.DefaultRequestHeaders.Contains("Authorization"))
                HttpClient.DefaultRequestHeaders.Add("Authorization", token);
            logoutDisply = "block";
            userInfo = JsonSerializer.Deserialize<UserInfoDto>(await JSRuntime.InvokeAsync<string>("localStorage.getItem", "userinfo"));
            if (userInfo.Id != Guid.Empty)
                HeadUrl = userInfo.HeadPhoto;
        }
        else
        {
            HeadUrl = "../img/defaultHeadPhoto.jpg";
            logoutDisply = "none";
        }
        await base.OnInitializedAsync();
    }

    public async Task OnSearch()
    {
        string token = await JSRuntime.InvokeAsync<string>("getCookieByName", "XSRF-TOKEN");
        HttpClient.DefaultRequestHeaders.Add("requestverificationtoken", token);
        var res = await HttpClient.GetAsync("/api/blog/my-account/user-info");
        Console.WriteLine(await res.Content.ReadAsStringAsync());
    }

    public async Task Logout()
    {
        //await sessionStorage.RemoveItemAsync("Authorization");
        await JSRuntime.InvokeVoidAsync("localStorage.clear");
        HttpClient.DefaultRequestHeaders.Remove("Authorization");
        await JSRuntime.InvokeVoidAsync("openNew", AuthUrl + "/account/logout", "_blank",true);
        //navigationManager.NavigateTo(navigationManager.Uri, true);
    }

    public async Task HeadNavTo()
    {
        if (!string.IsNullOrEmpty(token))
        {
            navigationManager.NavigateTo($"/usercenter/{userInfo.UserName}");
        }
        else
        {
            var authUrl = AuthUrl + $"/connect/authorize?response_type=code&client_id=Blog_Blazor&redirect_uri={SelfUrl}/login-callback&scope=Blog";
            await JSRuntime.InvokeVoidAsync("openNew", authUrl, "_blank");
        }
    }
}

